"""
批量写操作
"""
import pymysql

kwargs = {
    'host': 'localhost',
    'port': 3306,
    'user': 'root',
    'password': '123456',
    'database': 'stu',
    'charset': 'utf8'
}
# 连接数据库\生成游标
db = pymysql.connect(**kwargs)  # 解压字典传参
cur = db.cursor()

# 操作数据 批量写
data = [
    ('xxx', '19', 'f', 55),
    ('yyy', '21', 'f', 65),
    ('zzz', '23', 'm', 75)
]
try:
    # sql = "insert into class (name,age,sex,score) values (%s,%s,%s,%s);"
    sql = "insert into class " \
          "(name,age,sex,score) " \
          "values (%s,%s,%s,%s);"
    cur.executemany(sql, data)  # 批量写入

    db.commit()
except Exception as e:
    print(e)
    db.rollback()

# 关闭游标\断开数据库连接
cur.close()
db.close()
